// spForm.js

// our javascripts namespace
var spForm = {};

///////////////////////////////////////////////////////////////////////////////////////////////////
// init
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.init = function(){

	jQuery( ".spFormControllerLoad" ).each(function(){
		spForm.load( jQuery(this) );
	});
	
	// handle overview group form click
	jQuery( "body" ).on( "click", ".ttfEditable", function() {
		spForm.showForm( jQuery(this) );
	});
	
	// handle edit
	jQuery( "body" ).on( "click", ".spFormControllerEdit", function(e){
		e.preventDefault();
		spForm.edit( jQuery(this) );
	});
	
	// handle save
	jQuery( "body" ).on( "click", ".spFormControllerSave", function(e){
		e.preventDefault();
		spForm.save( jQuery(this) );
	});		
	
	// handle create
	jQuery( "body" ).on( "click", ".spFormControllerCreate", function(e){
		e.preventDefault();
		spForm.create( jQuery(this) );
	});		
	
	// handle delete
	jQuery( "body" ).on( "click", ".spFormControllerDelete", function(e){
		e.preventDefault();
		spForm.onDelete( jQuery(this) );
	});		

	// handle trash
	jQuery( "body" ).on( "click", ".spFormControllerTrash", function(e){
		e.preventDefault();
		spForm.onTrash( jQuery(this) );
	});		
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// onTrash
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.onDelete = function( element ){

	var dbClass = element.attr( "data-dbclass" );
	var id 		= element.attr( "data-id" );
	var name 	= element.attr( "data-name" );
	
	if ( confirm( 'Are you sure you want to trash: '+name ) )
	{
		// delete
		spAjaxCallback({ 
			callback 	: "spFormController::trashFormRequest", 
			param		: [
					{ name:'id',		value:id },
					{ name:'dbclass', 	value:dbClass }
				],
			title		: 'Trashing...',
			success		: function(){
				// we need to reload the page because more than one group can be effected by changes
				location.reload( true );
			}
		});
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// onDelete
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.onDelete = function( element ){

	var dbClass = element.attr( "data-dbclass" );
	var id 		= element.attr( "data-id" );
	var name 	= element.attr( "data-name" );
	
	if ( confirm( 'Are you sure you want to delete: '+name ) )
	{
		// delete
		spAjaxCallback({ 
			callback 	: "spFormController::deleteFormRequest", 
			param		: [
					{ name:'id',		value:id },
					{ name:'dbclass', 	value:dbClass }
				],
			title		: 'Deleting...',
			success		: function(){
				// we need to reload the page because more than one group can be effected by changes
				location.reload( true );
			}
		});
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// create
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.create = function( element ){

	var containerName 	= element.attr( "data-container" );
	var dbClass	 		= element.attr( "data-dbclass" );
	var container = jQuery(containerName);
	
	// this might be a one to many form, in which case we need to supply relationcol and parentid
	var relationCol		= element.attr( "data-relationcol" );
	var parentId		= element.attr( "data-parentid" );
	
	spShowLoadingBar( container );
	container.load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: 'spFormController::showFormCreateRequest',
			dbclass			: dbClass,
			containername	: containerName,
			relationcol		: relationCol,
			parentid		: parentId
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
		}
	);	
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// load
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.load = function( element ){

	var load 	= element.attr( "data-load" );
	var id		= element.attr( "data-id" );

	spShowLoadingBar( element );
	element.load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: load,
			id				: id
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
		}
	);	
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// showForm
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.showForm = function( element ){

	var containerName 	= element.attr( "data-container" );
	var id 				= element.attr( "data-id" );
	var dbClass	 		= element.attr( "data-dbclass" );;
	
	var container = jQuery(containerName);
	
	spShowLoadingBar( container );
	container.load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: 'spFormController::showFormRequest',
			dbclass			: dbClass,
			id				: id,
			containername	: containerName
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
		}
	);	
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// edit
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.edit = function( element ){

	var containerName 	= element.attr( "data-container" );
	var id 				= element.attr( "data-id" );
	var dbClass	 		= element.attr( "data-dbclass" );;
	var container = jQuery(containerName);
	
	spShowLoadingBar( container );
	container.load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: 'spFormController::editFormRequest',
			dbclass			: dbClass,
			id				: id,
			containername	: containerName
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
		}
	);	
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// save
///////////////////////////////////////////////////////////////////////////////////////////////////

spForm.save = function( anchor )
{
	spOpenDialog( "Saving..." );

	var containerName 	= anchor.attr( "data-container" );
	var id 				= anchor.attr( "data-id" );
	var dbClass 		= anchor.attr( "data-dbclass" );

	var container = jQuery(containerName);
	
	var form = container.find( "form:first" );
	var ser	= form.serialize();
	
	jQuery.ajax({
		url 		: spHome+"/wp-admin/admin-ajax.php?action=sp_ajax_response",
		data 		: ser+"&formType=none&callback=spFormController::editSave&dbclass="+dbClass+"&id="+id,
		dataType 	: "json",
		type 		: "POST",
		success		: function(data){
			
			if ( data.formerrors!=undefined )
			{
				spCloseDialog();
				processFormErrors( data.formerrors );
				return;
			}

			// we need to reload the page because more than one group can be effected by changes
			location.reload( true );
		},
		error 		: function(XMLHttpRequest, textStatus, errorThrown){
			jQuery( "#spErrorMsg" ).html( 
				"AJAX ERROR:<br/><textarea>"+XMLHttpRequest.status+"</textarea>" +
				"<textarea>"+textStatus+"</textarea>" +
				"<textarea>"+XMLHttpRequest.responseText+"</textarea>"+
				"<textarea>"+errorThrown+"</textarea>"
			);
			
			jQuery( "#spErrorMsg" ).html( XMLHttpRequest.responseText );
		},
		complete 	: function()
		{
			//spCloseDialog();
		}
	});		

	return false;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// call init when ready
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery(document).ready(function(){
	spForm.init();
});